<?php

/**
 * PreconditionFailed 
 *
 * This exception is normally thrown when a client submitted a conditional request, 
 * like for example an If, If-None-Match or If-Match header, which caused the HTTP 
 * request to not execute (the condition of the header failed)
 * 
 * Copyright © 2010 Jack Cleaver
 * 
 * This plugin provides functionality added by CalDAV (RFC 4791)
 * It implements new reports, and the MKCALENDAR method.
 *
 * This file is part of Slash.
 *
 * Slash is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Slash is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_ACL_Exception_NeedPrivileges extends Sabre_DAV_Exception_PreconditionFailed 
{
	
	private $resources;

    /**
     * Create the exception 
     * 
     * @param string $message 
     * @param string $header 
     */
    public function __construct($resources) 
    {
        parent::__construct("Insufficient privileges");
        $this->resources = $resources;

    } 

    /**
     * Returns the HTTP statuscode for this exception 
     *
     * @return int
     */
    public function getHTTPCode() {

        return 403; 

    }

    /**
     * This method allows the exception to include additonal information into the WebDAV error response 
     *
     * @param Sabre_DAV_Server $server
     * @param DOMElement $errorNode 
     * @return void
     */
    public function serialize(Sabre_DAV_Server $server,DOMElement $errorNode) 
    {
    	$prefix = $server->xmlNamespaces['DAV:'];
    	$doc = $errorNode->ownerDocument; 
    	$np = $doc->createElement("$prefix:need-privileges");
    	foreach ($this->resources as $resource)
    	{
    		$resElement = $doc->createElement("$prefix:resource");
    		
    		$href = $doc->createElement("$prefix:href");
    		$txt = $doc->createTextNode($resource["href"]);
    		$href->appendChild($txt);
    		$resElement->appendChild($href);
    		
    		$privElement = $doc->createElement("$prefix:privilege");
    		$priv = $doc->createElement("$prefix:" . $resource["privilege"]);
    		$privElement->appendChild($priv);
    		$resElement->appendChild($privElement);
    		
    		$np->appendChild($resElement);
    	}
      $errorNode->appendChild($np);
    }

}
